<?php


namespace Api\Controller;

// +----------------------------------------------------------------------
// | 文件 GameController.class.php
// +----------------------------------------------------------------------
// | 说明 游戏相关接口
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

class GameController extends PublicController 
{

    static $filter = [
            'id', 'name', 'pic', 'background', 'scribe', 'is_scribe', 'logo',
        ];

    // 给PC提供的
    public function pc()
    {
        $map = [
            'client_type' => 'pc'
        ];
        $gEvent = A('Api/Game', 'Event');
        $games = $gEvent->showLists($map);
        $uid = $this->getUid(I('appToken'));
        foreach ($games as &$game) {
            $this->_format_game($game);
            $data[] = $game;
        }
        $this->ajaxReturn($data);
    }

    // 给手机端提供
    public function mobile()
    {
        $this->all();
    }

    // 返回所有
    public function all()
    {
        $gEvent = A('Api/Game', 'Event');
        $games = $gEvent->showLists();
        $uid = $this->getUid(I('appToken'));

        foreach (C('client_type') as $key => $name) {
            $res[$key] = [
                'title' => $name,
            ];
        }
        foreach ($games as $game) {
            $client_type = !empty($game['client_type']) ? $game['client_type'] : [];
            $this->_format_game($game);
            if ($client_type){
                foreach ($client_type as $client) {

                    $res[$client]['games'][] = $game;
                }
            }
        }
        $this->ajaxReturn($res);
    }

    protected function _format_game(&$game, $uid = 0)
    {
        uri_format($game['pic']);
        uri_format($game['logo']);
        uri_format($game['background']);
        $uid && $game['is_scribe'] = intval($gsEvent->is_scribe($uid, $game['id']));
        $game['scribe'] = intval($game['scribe']);
        $game = field_filter($game, self::$filter);
    }



}